package com.xls.boxlibrary.dilog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.orhanobut.logger.Logger;
import com.xls.boxlibrary.R;
import com.xls.boxlibrary.utils.TUtils;
import com.xls.commonlibrary.ad.adn.gm.GMFeedUtil;
import com.xls.commonlibrary.bean.ShowEcpmResult;

// todo 底部加信息流广告，显示为3个数据
public class JiFenDialog extends Dialog {


    AppCompatActivity activity;
    private FrameLayout mFeedContainer;

//    private ImageView ic_dialog_jifen;
//    private ImageView ic_dialog_fbzbt;
//    private ImageView iv_three;
    private TextView dialog_jifen_hint;
    private TextView dialog_jifen_hb;
//    private TextView dialog_jifen_xz;
//    private TextView dialog_jifen_fanbei;
//    private RelativeLayout dialog_jifen_rlfanbei;
//    private TextView dialog_jifen_zhijieshouxia;
//    String jifen = "";
//    String jinbi = "";
    ShowEcpmResult ecpmResult;

    ValueCallback<String> valueCallback;
    private RelativeLayout dialog_jifen_zhijieshouxia2;
//    private RelativeLayout ic_dialog_yuanbao;
//    int showCount = 0;// // 广告显示计数

    /**
     * @param context
     * @param showCount
     * @param callback  我要翻倍 按钮点击回调
     */
    public JiFenDialog(Context context,
//                       String jifen, String jinbi,
                       ShowEcpmResult info,
                       int showCount, ValueCallback<String> callback) {
        super(context, R.style.Splash);

        this.activity = (AppCompatActivity) context;

//        this.jifen = jifen;
//        this.jinbi = jinbi;
        this.ecpmResult = info;
//        this.showCount = showCount;
        this.valueCallback = callback;

        Logger.d("======showPointDialog=====JiFenDialog======");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        getWindow().setContentView(R.layout.dialog_jifen);
        initView();
    }

    private void initView() {
        mFeedContainer = findViewById(R.id.fl_content);
//        ic_dialog_jifen = findViewById(R.id.ic_dialog_jifen);
//        iv_three = findViewById(R.id.iv_three);
//        ic_dialog_fbzbt = findViewById(R.id.ic_dialog_fbzbt);
        dialog_jifen_hint = findViewById(R.id.dialog_jifen_hint);
        dialog_jifen_hb = findViewById(R.id.dialog_jifen_hb);
//        dialog_jifen_xz = findViewById(R.id.dialog_jifen_xz);
//        dialog_jifen_fanbei = findViewById(R.id.dialog_jifen_fanbei);
//        dialog_jifen_rlfanbei = findViewById(R.id.dialog_jifen_rlfanbei);
//        dialog_jifen_zhijieshouxia = findViewById(R.id.dialog_jifen_zhijieshouxia);
//        dialog_jifen_zhijieshouxia.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (valueCallback != null) {
//                    // 直接收下返 -1
//                    valueCallback.onReceiveValue("-1");
//                }
//                dismiss();
//            }
//        });

//        dialog_jifen_hb.setText(jifen);
//        dialog_jifen_xz.setText(jinbi);
//        dialog_jifen_hb.setText("应得元宝  " + this.ecpmResult.getShouldScore() +
//                "\n奖励  " + this.ecpmResult.getSubsidyScore() +
//                "\n实得元宝  " + this.ecpmResult.getCurrentScore()
//        );
        Logger.d("========JiFenDialog=========应得元宝==========showCount:" + this.ecpmResult.getShouldScore());
        Logger.d("========JiFenDialog=========奖励==========showCount:" + this.ecpmResult.getSubsidyScore());
        Logger.d("========JiFenDialog=========实得元宝==========showCount:" + this.ecpmResult.getCurrentScore());

//        if (TextUtils.equals(this.ecpmResult.getSubsidyScore(),"0")||TextUtils.isEmpty(this.ecpmResult.getSubsidyScore())){
////            dialog_jifen_hint.setText("总获得:" + this.ecpmResult.getCurrentScore()+"元宝");
//            dialog_jifen_hb.setText(this.ecpmResult.getShouldScore()+"元宝");
//        }else {
////            dialog_jifen_hint.setText("总获得:" + this.ecpmResult.getCurrentScore()+"元宝");
//            dialog_jifen_hb.setText( this.ecpmResult.getShouldScore()+"元宝(奖励："+this.ecpmResult.getSubsidyScore()+"元宝)");
//        }
//        dialog_jifen_xz.setText(this.ecpmResult.getScore() + "金币");
        dialog_jifen_hb.setText(this.ecpmResult.getCurrentScore()+"元宝");

//        dialog_jifen_fanbei.setOnClickListener(view -> {
//
//            if (valueCallback != null) {
//                valueCallback.onReceiveValue("");
//            }
//            dismiss();
//
//        });
        dialog_jifen_zhijieshouxia2 = findViewById(R.id.dialog_jifen_zhijieshouxia2);
        dialog_jifen_zhijieshouxia2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (valueCallback != null) {
                    // 直接收下返 -1
                    valueCallback.onReceiveValue("-1");
                }
                dismiss();
            }
        });
//        ic_dialog_yuanbao = findViewById(R.id.ic_dialog_yuanbao);


        // 差异化UI，一定放到initView()的最后
        // 显示次数，3次均不同

//        Logger.d("========JiFenDialog=========差异化UI==========showCount:" + this.showCount);
//        if (this.showCount == 1) {
//            // 右上图片 翻倍3次  显示
//            iv_three.setVisibility(View.VISIBLE);
//            iv_three.setImageResource(R.drawable.ic_dialog_jifen_fanbei);
//
//            // 我要翻倍按钮  显示
//            dialog_jifen_rlfanbei.setVisibility(View.VISIBLE);
//
//        } else if (this.showCount == 2) {
//            // 右上图片 翻倍2次 显示
//            iv_three.setVisibility(View.VISIBLE);
//            iv_three.setImageResource(R.drawable.ic_dialog_jifen_fanbei2);
//
//            // 我要翻倍按钮  显示
//            dialog_jifen_rlfanbei.setVisibility(View.VISIBLE);
//
//        } else if (this.showCount == 3) {
//            // 右上图片 翻倍1次 显示
//            iv_three.setVisibility(View.VISIBLE);
//            iv_three.setImageResource(R.drawable.ic_dialog_jifen_fanbei3);
//
//            // 我要翻倍按钮  显示
//            dialog_jifen_rlfanbei.setVisibility(View.VISIBLE);
//
//        } else {
//            // 右上图片 翻倍隐藏
//            showZhijieShowXia();
//        }


        showFeed();

    }

    void showFeed() {
        // todo 信息流广告
        GMFeedUtil.getInstance().loadFeed(
                this.activity,
                mFeedContainer,
                result -> {

                }
        );
    }


//TODO   ic_dialog_jifen_fanbei2   ic_dialog_jifen_fanbei3

    /**
     * 显示最后一次翻倍  只接收下
     */
//    private void showZhijieShowXia() {
//        dialog_jifen_zhijieshouxia2.setVisibility(View.VISIBLE);
//        dialog_jifen_rlfanbei.setVisibility(View.INVISIBLE);
//        iv_three.setVisibility(View.INVISIBLE);
//        dialog_jifen_zhijieshouxia.setVisibility(View.INVISIBLE);
//    }
}
